package com.szgr.baoyin.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.szgr.baoyin.model.City;
import com.szgr.baoyin.myview.indexbar.PinnedSectionListView;
import com.szgr.baoyin.utils.StringMatcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 15/9/29.
 */
public class ChooseCityListAdapter extends BaseAdapter implements SectionIndexer,PinnedSectionListView.PinnedSectionListAdapter{
    public static int TYPE_ITEM = 0;
    public static int TYPE_SECTION = 1;
    private List<Item> mItems = new ArrayList<>();
    private Map<String,Character> mCityMap;
    private List<String> mSections = new ArrayList<>();
    private Context mContext;
    public ChooseCityListAdapter(Context context, List<City> citys){
        mContext = context;
        if(mCityMap == null){
            buildCityMap(citys);
        }
        Collections.sort(citys, new CityComparator<City>());

        char prevChar = '.';
        Item item = null;
        for (City city : citys) {
            char fc = lookupFirstChar(city.getId());
            if(fc != prevChar){
                mSections.add(String.valueOf(fc));
                prevChar = fc;

                item = new Item();
                item.setType(TYPE_SECTION);
                item.setText(String.valueOf(fc));
                item.setCity(city);
                mItems.add(item);
            }

            item = new Item();
            item.setType(TYPE_ITEM);
            item.setText(city.getName());
            item.setCity(city);
            mItems.add(item);
        }

    }

    private char lookupFirstChar(String id) {
        return mCityMap.get(id);
    }

    private void buildCityMap(List<City> citys) {
        mCityMap = new HashMap<>();
        for (City city : citys) {
            mCityMap.put(city.getId(), Character.toUpperCase(city.getPinyin().charAt(0)));
        }
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position).getCity();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    android.R.layout.simple_list_item_1, parent, false);
        }
        int type = getItemViewType(position);
        if (isItemViewTypePinned(type)) {
            ((TextView)view).setText(mItems.get(position).text);
            view.setBackgroundColor(0xffBBBBBB);
            ((TextView)view).setTextColor(0xff333333);
        } else {
            ((TextView)view).setText(mItems.get(position).text);
            view.setBackgroundColor(Color.WHITE);
            ((TextView)view).setTextColor(0xff000000);
        }
        return view;
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == TYPE_SECTION;
    }

    @Override
    public Object[] getSections() {
        String[] strs = new String[mSections.size()];
        for (int i = 0; i < mSections.size(); i++) {
            strs[i] = mSections.get(i);
        }
        return strs;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = sectionIndex; i >= 1; i--) {
            for (int j = 0; j < getCount(); j++) {
                if (i == 0) {
                    // For numeric section
                    for (int k = 0; k <= 9; k++) {
                        if (StringMatcher.match(mItems.get(j).getText(), String.valueOf(k))) {
                            return j -1;
                        }
                    }
                } else {
                    char fc = lookupFirstChar(((City) getItem(j)).getId());
                    if (StringMatcher.match(String.valueOf(fc), mSections.get(i))) {
                        return j ;
                    }
                }
            }
        }
        return 0;
    }

    @Override
    public int getSectionForPosition(int position) {
        for (int i = 0; i < mSections.size(); i++) {
            char fc = lookupFirstChar(mItems.get(position).getCity().getId());
            if(mSections.get(i).equals(String.valueOf(fc))){
                return i;
            }
        }
        return 0;
    }

    public class Item{
        private int type;
        private String text;
        private City city;


        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public City getCity() {
            return city;
        }

        public void setCity(City city) {
            this.city = city;
        }
    }

    public class CityComparator<T> implements Comparator<T>{

        @Override
        public int compare(T lhs, T rhs) {
            if(lhs instanceof City && rhs instanceof City){
                City lc = (City) lhs;
                City rc = (City) rhs;
                return lc.getPinyin().compareTo(rc.getPinyin());
            }
            return 0;
        }
    }
}
